<?php

namespace app\service;

use app\model\PartnerLogModel;
use app\model\WarehouseModel;
use think\facade\Db;

class WarehouseService
{
    public static function info()
    {
        $warehouseType = WarehouseModel::GetAllWarehouseType();
        $bussineType = WarehouseModel::GetAllBussineType();
        return [
            'warehouseType' => $warehouseType,
            'bussineType' => $bussineType
        ];
    }

    public static function lists($param)
    {
        return WarehouseModel::list($param);
    }

    public static function line_data($param)
    {
        return WarehouseModel::line_data($param);
    }

    public static function add($data, $logParam)
    {
        $gpsInfo = baidutogps($data['longitude'], $data['dimension']);
        if ($gpsInfo != 400) {
            $data['bd_gps_longitude'] = $gpsInfo[0]['x'];
            $data['bd_gps_dimension'] = $gpsInfo[0]['y'];
        }
        Db::startTrans();
        $data['add_time'] = getNowDateTime();
        $res = WarehouseModel::add($data);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '添加仓库',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function edit($data, $logParam, $id)
    {
        Db::startTrans();
        $data['update_time'] = getNowDateTime();
        $res = WarehouseModel::edit($data, $id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '修改仓库信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function del($data, $logParam, $id)
    {
        Db::startTrans();
        $res = WarehouseModel::del($id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '删除仓库信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
}